<template>
  <div class="search-result">
    <van-pull-refresh
      v-model="isLoading"
      @refresh="onRefresh"
      success-text="刷新成功"
      :head-height="80"
    >
      <van-cell v-for="item in resultList" :key='item.art_id' :title="item.title" @click="toPage(item.art_id)" />
    </van-pull-refresh>
  </div>
</template>

<script>
import { searchResultAjax } from "@/api/axios";
export default {
  name: "SearchResult",
  data() {
    return {
      resultList: [], //获取结果
      page: 1, //页数
      per_page: 10, //每页显示条
      isLoading: false,
    };
  },
  props: {
    keyword: { type: String },
  },
  methods: {
    async onRefresh() {
      try {
        const { data: res } = await searchResultAjax({
          page: this.page,
          per_page: this.per_page,
          q: this.keyword,
        });
        console.log(res);
        this.resultList.unshift(...res.data.results)
        this.isLoading = false;
        this.page++
      } catch (err) {
        console.log(err);
      }
    },
     toPage(id) {
      this.$router.push(`/article/${id}`);
    }
  },
  created() {
    this.onRefresh()
  },
};
</script>

<style scoped lang='less'>
.search-result {
  width: 100%;
  height: 100%;
  .van-pull-refresh {
    width: 100%;
    height: 100%;
    overflow: auto;
  }
}
</style>